<template>
  <div class="contail">
    <van-nav-bar class="header"
                 title="我的小区"
                 left-arrow
                 @click-left="goBack()" />
    <div class="fengniao-tip"
         v-if="tip">暂时没有数据</div>
    <div class="weaper">
      <div v-for="(item,index) in list"
           :key="index"
           class="fengniao-list">
        <div class="fengniao-content item"
             @click="setWuyeDefault(item,index)">
          <div class="header">
            <p class="title"> {{item.name}} </p>
            <el-tag type="danger"
                    v-if="item.id==defaultId"
                    class="default">默认</el-tag>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { getWuyeAuth, setWuyeDefault } from '@/api/user';
import { validatenull } from '@/util/validate';
import baseCommon from '@/mixins/common';
import Bus from '@/util/bus.js';
import { mapGetters } from 'vuex';

export default {
  name: 'communityContail',
  mixins: [baseCommon()],
  data () {
    return {
      list: [],
      tip: false,
      defaultId: {},
    };
  },
  created () {
    this.getList();
  },
  computed: {
    ...mapGetters(['userInfo']),
  },
  mounted () { },
  methods: {
    setWuyeDefault (item, index) {
      this.$store.commit('SET_COMMUNITY', {
        communityId: item.id,
        communityName: item.name,
      });
      setWuyeDefault(item.id, this.userInfo.userId)
        .then(() => this.$store.dispatch('GetUserInfo'))
        .then(() => {
          this.defaultId = item.id;
          Bus.$emit('refershIndex');
          this.$message.success({
            message: '切换成功',
            duration: 1500,
          });
        });
    },
    getList () {
      getWuyeAuth().then((res) => {
        const data = res.data;
        const obj = data.obj;
        const attributes = data.attributes;
        this.defaultId = attributes.default_community.communityId;
        this.list = obj;
      });
    },

  },
};
</script>

<style lang="scss" scoped>
</style>

